/****************************************************************
 * <strong>MFServletContextListener</strong>
 *
 * <p>
 * MFServletContextListener
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.listener;

import com.thinkwide.data.core.MFPath;
import com.thinkwide.data.debug.MFDebug;
import org.dom4j.DocumentException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Timer;

public class MFServletContextListener implements ServletContextListener {
    private Timer timer = null;

    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        if (timer != null) timer.cancel();
    }

    public void contextInitialized(ServletContextEvent arg0) {

        String path = MFPath.getDirFromClassLoader() + MFPath.fseparator + "mflistener.xml";
        MFListenerManager manager = new MFListenerManager();
        manager.setApplication(arg0.getServletContext());

        try {
            manager.loadConfig(path);
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        timer = new Timer();
        timer.schedule(manager, 1000, 1 * 1000);
        MFDebug.println("启动后台自动监听服务...");

        /**** 老式定时器方式
         String runState=arg0.getServletContext().getInitParameter("message-run");
         String sTime=arg0.getServletContext().getInitParameter("message-time");
         Integer iTime=MFConvert.strToIntDef(sTime, 20);
         if (MFString.compareNoCase(runState,"true"))
         {
         System.out.println("间隔"+sTime+"秒发送一次！");
         timer = new Timer();
         timer.schedule(new MFPhoneTask(iTime), 1000, iTime*1000);
         }
         ***/

    }

}
